package com.cskaoyan.controller.goods;

import com.cskaoyan.model.bo.PageBo;

import com.cskaoyan.model.bo.goods.categorybrand.CategoryAndBrandRoot;
import com.cskaoyan.model.bo.goods.create.GoodsCreateRootBO;
import com.cskaoyan.model.bo.goods.delete.GoodsDeleteBO;
import com.cskaoyan.model.bo.goods.list.GoodsListPageBo;
import com.cskaoyan.model.bo.goods.update.GoodsUpdateRoot;
import com.cskaoyan.model.vo.goods.GoodsBaseData;
import com.cskaoyan.model.vo.BaseRespVo;
import com.cskaoyan.model.vo.goods.detail.GoodsDetailData;
import com.cskaoyan.service.goods.GoodsService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @program: project-market
 * @description: 商品管理模块Controller     /admin/goods/***
 * @author: Jesson Guo
 * @create: 2021-11-29 20:39
 **/
@RequestMapping("admin/goods")
@RestController
public class GoodsController {
    @Autowired
    GoodsService goodsService;

    //功能1
    // 所有商品列表显示
    // url---> /goods/list
    @RequiresPermissions("admin:goods:list")
    @GetMapping("list")
    public BaseRespVo getGoodsList(GoodsListPageBo goodsListPageBo) {
        if (goodsListPageBo.getLimit()==0){return BaseRespVo.ok();}
        else {
            GoodsBaseData goodsBaseData = goodsService.allGoodsList(goodsListPageBo);
            return BaseRespVo.manual(0, null, goodsBaseData);
        }

    }


    //功能6 新增商品
    //url---> /goods/create
    @RequiresPermissions("admin:goods:create")
    @PostMapping("create")
    public BaseRespVo createNewGood(@RequestBody GoodsCreateRootBO goodsCreateRootBO) {
        int code = goodsService.createNewGood(goodsCreateRootBO);
        if (code == 200) return BaseRespVo.ok();
        if (code == 211) return BaseRespVo.manual(611, "商品名已存在", null);
        else return BaseRespVo.manual(611,"参数违规,本次提交已驳回,请修正",null);

    }

    //功能2 新增商品时,品牌种类等数据回显
    @GetMapping("catAndBrand")
    public BaseRespVo catAndBrand() {
        CategoryAndBrandRoot categoryAndBrandRoot = goodsService.catAndBrand();
        return BaseRespVo.ok(categoryAndBrandRoot);
    }

    //功能3 删除商品
    @RequiresPermissions("admin:goods:delete")
    @PostMapping("delete")
    public BaseRespVo deleteGood(GoodsDeleteBO goodsDeleteBO) {
        int code = goodsService.deleteGood(goodsDeleteBO);
        if (code == 200) return BaseRespVo.ok();
        else return BaseRespVo.manual(611, "商品已删除", null);
    }

    //功能4 修改商品信息时,商品信息回显
    @RequiresPermissions("admin:goods:detail")
    @GetMapping("detail")
    public BaseRespVo detail(@Param("id") Integer id) {
        GoodsDetailData goodsDetailData =goodsService.getGoodDetail(id);
        return BaseRespVo.ok(goodsDetailData);
    }

    //功能5 修改商品信息的上传
    @RequiresPermissions("admin:goods:update")
    @PostMapping("update")
    public BaseRespVo update(@RequestBody GoodsUpdateRoot goodsUpdateRoot){
        int code=goodsService.updateGood(goodsUpdateRoot);
        if (code==200)return BaseRespVo.ok();
        else return BaseRespVo.manual(611,"参数违规,本次非法操作已回退,亲勿忧", null);
    }
}
